Linux下protobuf的安装过程&简单使用。
protobuf是google开发的一个灵活的、高效的用于序列化数据的协议。相比较XML和JSON格式,protobuf更小、更快、更便捷。
安装
- 下载链接:https://github.com/protocolbuffers/protobuf/releases ,我是用c++,所以下载protobuf-cpp就行了。
- tar -xzvf protobuf-*.tar.gz
- cd protobuf-*
- ./configure –prefix=/usr/local/protobuf
- make (可能会等好一段时间)
- make check
- make install
vim /etc/profile
1
2export PATH=$PATH:/usr/local/protobuf/bin/
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/source /etc/profile
- protoc –version #测试是否安装成功
使用方法
1 | cd ~/test |
vim address.proto
1
2
3
4
5
6
7
8
9
10package tutorial;
message Persion {
required string name = 1;
required int32 age = 2;
}
message AddressBook {
repeated Persion persion = 1;
}protoc -I=. –cpp_out=. address.proto #在当前文件夹生成cpp的.h和.cc文件
- vim write.cpp
1 |
|
read.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using namespace std;
void ListPeople(const tutorial::AddressBook& address_book) {
for (int i = 0; i < address_book.persion_size(); i++) {
const tutorial::Persion& persion = address_book.persion(i);
cout << persion.name() << " " << persion.age() << endl;
}
}
int main(int argc, char **argv) {
//GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2) {
cerr << "Usage: " << argv[0] << " ADDRESS_BOOL_FILE" << endl;
return -1;
}
tutorial::AddressBook address_book;
{
fstream input(argv[1], ios::in | ios::binary);
if (!address_book.ParseFromIstream(&input)) {
cerr << "Filed to parse address book." << endl;
return -1;
}
input.close();
}
ListPeople(address_book);
// Optional: Delete all global objects allocated by libprotobuf.
//google::protobuf::ShutdownProtobufLibrary();
return 0;
}编译两个文件
1
2g++ address.pb.cc write.cpp -o write `pkg-config --cflags --libs protobuf`
g++ address.pb.cc read.cpp -o read `pkg-config --cflags --libs protobuf`运行
1
2
3
4./write data.txt
aaa 999
./read data.txt
aaa 999
问题
- 运行write时,提示找不到动态链接库
1
2添加安装路径下的lib路径
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib
编码示例
1 | package test |
参考
- https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/
- https://www.cnblogs.com/luoxn28/p/5303517.html
- https://developers.google.com/protocol-buffers/docs/cpptutorial
- https://www.cnblogs.com/silvermagic/p/9087593.html
- Protobuf通信协议详解:代码演示、详细原理介绍等
欢迎与我分享你的看法。
转载请注明出处:http://taowusheng.cn/